package com.blb.day3;

public class WaitNotifyTest {

    //锁对象
    static Object lock = new Object();

    public void func(){
        //lock作为锁对象
        synchronized (lock){
            for(int i = 0;i < 10;i++) {
                System.out.println(Thread.currentThread().getName()+"---"+i);
                if(i == 3) {
                    //让当前线程进入等待
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        WaitNotifyTest test = new WaitNotifyTest();
        for(int i = 0;i < 3;i++) {
            new Thread(() -> {
                test.func();
            }).start();
        }
        Thread.sleep(3000);
        //锁对象通知等待线程执行
        synchronized (lock) {
            lock.notifyAll();
        }
    }
}
